package Classes.GameViews.ViewPanels
{
	import Classes.GameDatas.GD_Shop;
	import Classes.GameUtils.GD_deal;
	import Classes.GameViews.ViewScenes.VS_Main;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	
	import UIComponents.IGD_ShopItem;
	import UIComponents.Messages.PanelEvent;
	
	import flash.display.Sprite;
	import flash.system.ApplicationDomain;
	
	public class VP_message extends ViewPanel
	{
		private var _interfaceUI:Sprite;
		private static var _instance:VP_message;
		private var _newMessageCount:uint=0;
		public function VP_message()
		{
			super();
			this.init();
		}
		
		public function init():void
		{
			this._interfaceUI=new (ApplicationDomain.currentDomain.getDefinition("MessageUI_v") as Class);
			this.addChild(this._interfaceUI);
			addListener();
		}
		
		/**
		 *加一条消息来自object服务器 
		 * @param messageObj
		 * 
		 */
		public function addMessageFromObject(mObj:Object):void
		{
			var um:Object	=	new Object();
			um.ztId				=	mObj.ztId;
			um.ztItemId			=	mObj.ztItemId;
			um.ztItemCount		=	mObj.ztItemCount;
			um.ztFriendName		=	mObj.ztFriendName;
			um.ztFriendPic  	=	mObj.ztFriendPic;
			um.ztCommond		=	mObj.ztCommand;
			um.ztMessageType	=	uint(mObj.ztMessageType);
			
			var t:Array=mObj.ztMessageTime.split("-");
			var date:Date=new Date(t[0],(t[1]),t[2],t[3],t[4],t[5]);
			um.ztMessageTime	=	date;
			um.ztStatus			=	!uint(mObj.ztStatus);
			
			
			if(um.ztStatus)this._newMessageCount++;	
			
			var itemst:String	=	"";
			if(um.ztItemId==1)
				itemst	=	"金币";
			else 
				if(um.ztItemId==2)
					itemst	=	M_ConfigData.instance.isOpenQQVip?"q币":"o币";
				else{
					itemst	=	GD_deal.instance.getDataById(um.ztItemId).itemName;//"物品号为:"+um.item_id+"的物品";
					//					var iui:IUseItem =  GD_deal.instance.getDataByIdAndType(um.ztItemId,GD_deal.instance.getStr(um.ztItemId) );
					//					itemst	=	iui.itemName;
					//					itemst="xx";
				}
			
			
			switch(um.ztMessageType)//消息类型（3为消费，4为互动，5为求赠，1为礼物）
			{
				case 1:						//礼物
					um.Content		=	um.ztFriendName+"送给你"+itemst+" "+um.ztItemCount.toString();					
					break;			
				case 5:						//求赠
					um.Content		=	um.ztFriendName+"要求你打发点"+itemst+" "+um.ztItemCount.toString();					
					break;	
				case 4:						//互动
					um.Content		=	um.ztFriendName+"在你家做了"+um.ztCommond;					
					break;
				case 3:						//付费
					var igs:IGD_ShopItem	=	GD_Shop.instance.getShopInfoByItemInfo(um.ztItemId);
					if(null==igs)igs		=	GD_Shop.instance.getShopInfoByItemInfo(um.ztItemId,1);
					
					um.Content		=	"你用了"+ (igs.moneyType==0?"欧币":"金币")+igs.price+"买了"+um.ztItemCount+"件"+igs.itemName;					
					break;	
			}
			
			this._interfaceUI["screen"]["addItem"](um);
		}
		
		/**
		 *加多条消息 
		 * @param messageObjectAry
		 * 
		 */
		public function addMessageObjectFromAry(messageObjectAry:Array):void
		{
			for each(var obj:Object	in	messageObjectAry)
			{    
				//				this._newMessageCount++;
				addMessageFromObject(obj);
			}
			
			//			this._interfaceUI["screen"]["messageFilter"]();
			//			this._interfaceUI["screen"]["reShow"]();
			this._interfaceUI["screen"]["showAll"]();
			
			VP_Main.instance.newMessage	= this._newMessageCount;
		}
		
		public function cleanMessage():void
		{
			this._interfaceUI["screen"]["cleanMessage"]();
			this._newMessageCount=0;
		}
		
		private function addListener():void
		{
			this.addEventListener("messageClose",closeHandler);
			this.addEventListener("clickMessage",clickMessageHD);
		}
		
		private function clickMessageHD(e:PanelEvent):void
		{
			M_Communication.instance.send(124004,{ztId:e._id});
			//			this._newMessageCount--;
			//			VP_Main.instance.newMessage	= this._newMessageCount;
			
			
		}
		/**
		 * 关闭消息窗口 
		 * @param e
		 * 
		 */
		public function closeHandler(e:PanelEvent):void
		{
			this.close();
		}
		
		public function openPanel():void
		{
			this.openAt(VS_Main.instance,true);
			this.toCenter();
			
		}
		
		public static function get instance():VP_message
		{
			if(!_instance)_instance=new VP_message();
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		//		override public function get backPosition():Point
		//		{
		//			this._backPosition  =	(VP_Main.instance.interfaceUI["Message"] as Sprite).localToGlobal(new Point(0,0));//new  Point( (VP_Main.instance.interfaceUI["Message"] as Sprite).x, (VP_Main.instance.interfaceUI["Message"] as Sprite).y);
		//			return this._backPosition;
		//		}
		
		/**
		 * 设置id条消息是否显示按扭
		 */
		public function setShowBtn(id:uint,isShow:Boolean=true):void
		{
			if(!isShow){
				this._newMessageCount--;
				VP_Main.instance.newMessage	= this._newMessageCount;
			}
			this._interfaceUI["screen"]["setShowBtn"](id,isShow);
		}
		
		public function deleteMessageItem(id:uint):void
		{
			this._interfaceUI["screen"]["deleteMessageItem"](id);
		}
	}
}